<?php
/*
 * Creative Bulletin Board Generation 3
 * Version 1.0 Beta 1
 +------------------------------------
 * Core initialization Script
 * Started: August 22, 2009 10:01:24 AM
 * Script version: 1.0.5
 */

// The clock is ticking!
$timer['bootstrap_start'] = round(microtime(), 3);
/**** USER EDITABLE CONSTANTS ****/

// Development Setting
// Set to FALSE unless you know what you're doing!
// Setting to TRUE will report all errors with complete stacktrace for dev.
define("IN_DEV", true);

// Windows Mode
// Default is FALSE
// Set to TRUE if you're running WAMP
define("IN_WINDOWS", false);

// Legacy Mode
// Default is FALSE
// If you are used to all the 'facile' THT (http://thehostingtool.com) and CBB Generation 2 (Discontinued)
// coding, enable this.
// A full documentation is in /Documentation/legacy_mode.txt
define("LEGACY_MODE", false);

// Configuration File Name (relative to CORE_PATH, no slashes)
// Default conf.inc.php
// View documentation for details
define("CONFIG_FILE", "conf.inc.php");

// No more edits from here
/* Constants generated by this script */
define("IN_CBB", true);
define("CBB_VERSION", "1.0 Public Beta 1");
define("CF_VERSION", "1.0 Public Beta 1");
define("CF_VERSION_REAL", 10000);
define("BASE_PATH", realpath(dirname(__FILE__)."/../")."/");
define("FRONTEND_PATH", realpath(dirname(__FILE__)."/../applications/frontend")."/");
define("BACKEND_PATH", realpath(dirname(__FILE__)."/../applications/backend")."/");
define("UNIVERSAL_PATH", realpath(dirname(__FILE__)."/../applications/universal")."/");
define("PUBLIC_PATH", realpath(dirname(__FILE__)."/../public/")."/");
define("CORE_PATH", dirname(__FILE__)."/");
define("LIBRARY_PATH", CORE_PATH."libs/");
define("LAYOUT_PATH", realpath(dirname(__FILE__)."/../public/layout/".APP_TYPE)."/");
define("CONF_FILE", CORE_PATH.CONFIG_FILE);

// DEV
if(!IN_DEV){
	error_reporting(0);
}
else error_reporting(E_ALL);

require_once(CONF_FILE); # Call configuration file
require_once(CORE_PATH."exceptions.php");
require_once(CORE_PATH."debugger.php");
$dbg = new debugger;
/*
 * DB: Prefix
 */
define("DB_PREFIX", $config['db']['prefix']);
/*
 * Initialize our error and exception handlers */
set_exception_handler('handleException');
set_error_handler('handleError', E_ALL);

/* Initialize our classes.
 * This will call EVERYTHING in the /classes folder that is PHP, considering class name as the filename (minus .php)
 */
class cf{}
$_CF = new cf; // $_CF->CLASSNAME, $_CF->models->MODEL_NAME, implemented.

require_once(CORE_PATH."classes/db.php");
$db = new db;
$_CF->db = $db;

/*
 * Initialize MVC: Controllers
 * Just pulling up controllers. Again, you have to *register* the controllers in order to use them.
 */
$cQuery = $db->select("installed_controllers");
while($array = $db->fetch_array($cQuery)){
	$path = BASE_PATH."applications/universal/".$array['application']."/controllers/".$array['name'].".php";
	if(file_exists($path)){
		require_once($path);
	}
	else{
		trigger_error("Registered Controller {$array['name']} for application {$array['application']} not found!");
	}
}
$dir = opendir(CORE_PATH."classes");
if(!$dir){
	trigger_error("Cannot open ".CORE_PATH."classes/ folder. Is it uploaded and readable?");
}
else{
	while(($file = readdir($dir))!== false){
		if($file != "." and $file != ".."){
			$file = explode(".", $file, 2);
			if($file[1] == "php" and $file[0] !== "db"){ // DB was already MANUALLY included for... dev reasons.
				require_once(CORE_PATH."classes/{$file[0]}.php");
				${$file[0]} = new $file[0];
				$_CF->{$file[0]} = ${$file[0]};
			}
		}
	}
}

// Initialize (application-wise) classes.
$classes = $db->select("installed_applications", "*", 0, " WHERE `interface` = 'universal'");;
if(!$classes) trigger_error(mysql_error());
while($arr = $db->fetch_array($classes)){
	$direc = UNIVERSAL_PATH."{$arr['name']}/classes";
	$dir = opendir($direc);
	while(($file = readdir($dir)) !== false){
		if($file != "." and $file != ".."){
			$file = explode(".", $file, 2);
			if($file[1] == "php"){
				require_once(UNIVERSAL_PATH."{$arr['name']}/classes/{$file[0]}.php");
				${$file[0]} = new $file[0];
				$_CF->{$file[0]} = ${$file[0]};
			}
		}
	}
}

/*
 * Initialize MVC: Models
 * Models are still here as they require the core files immediately on runtime. Not controllers as they
 * are called later on :)
 */
class models{}
$_CF->models = new models;
$mQuery = $db->select("installed_models");
while($array = $db->fetch_array($mQuery)){
	$path = BASE_PATH."applications/universal/".$array['application']."/models/".$array['name'].".php";
	if(file_exists($path)){
		require_once($path);
		$models[$array['name']] = new $array['name'];
		$_CF->models->{$array['name']} = $models[$array['name']];
	}
	else{
		trigger_error("Registered Model {$array['name']} for application {$array['application']} not found!");
	}
}

//------------------------------------
// So lets call our user controller.
if(APP_TYPE == "backend"){
	if(isset($_SESSION["cf_admin_username"])) $_SESSION["cf_username"] = $_SESSION["cf_admin_username"];
}
if(isset($_SESSION["cf_username"])){
	if(isset($_SESSION["cf_controller"], $_SESSION["cf_controller_update_timestamp"])){
		if(time()-600 > $_SESSION["cf_controller_update_timestamp"]){ # Time to update my friend, you are too old!
			$userController = new user($_SESSION["cf_username"], "username");
			if($userController->status === false) unset($_SESSION["cf_username"]);
			$_SESSION["cf_controller_update_timestamp"] = time();
			$_SESSION["cf_controller"] = serialize($userController);
		}
	}
	elseif(!isset($_SESSION["cf_controller"])){
		$userController = new user($_SESSION["cf_username"], "username");
		if($userController->status === false) unset($_SESSION["cf_username"]);
		$_SESSION["cf_controller_update_timestamp"] = time();
		$_SESSION["cf_controller"] = serialize($userController);
	}
}

if(isset($_GET['makepass']) and IN_DEV){
	die($_CF->main->makePassword($_GET['makepass']));
}

// End clock 1
$timer['bootstrap_end'] = round(microtime(), 3);
